package com.jxwifi.cloud.quickcleanserver.utils;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${oushuhua} on 2017/6/19.
 */

public class DetectionUtils {

    public static class PopupWindows {

        /**
         * 构造一个PoputWindow
         */
        @SuppressLint("InlinedApi")
        public static PopupWindow getPoputWindow(View root, int... win) {

            // 装载R.layout.popup对应的界面布局

            PopupWindow popups = null;
            if (win.length > 0) {
                // 创建PopupWindow对象
                popups = new PopupWindow(root, win[0], android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                // 创建PopupWindow对象
                popups = new PopupWindow(root, ActionBar.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
            }
            final PopupWindow popup = popups;

            popup.setTouchable(true); // 设置popupwindow可点击
            popup.setOutsideTouchable(true); // 设置popupwindow外部可点击
            popup.setFocusable(true); // 获取焦点
            ColorDrawable cd = new ColorDrawable(-0000); // 设置背景
            popup.setBackgroundDrawable(cd);
            final View roots = root;
            popup.setTouchInterceptor(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    /**** 如果点击了popupupwindow的外部，popupupwindow也会消失 ****/
					/*
					 * if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					 * popup.dismiss();
					 *
					 * return true; } return false;
					 */
                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_OUTSIDE:
                            popup.dismiss();
                            return true;
                        case MotionEvent.ACTION_UP:
                            v.performClick();
                            break;
                    }
                    return false;
                }

            });

            return popup;

            // 级联显示 下拉框内容 end
        }

        /**
         * 构造一个PoputWindow
         */
        @SuppressLint("InlinedApi")
        public static PopupWindow getPoputWindowHeight(View root, final View view, int height) {

            // 装载R.layout.popup对应的界面布局
            // 创建PopupWindow对象
            PopupWindow popups = new PopupWindow(root, ActionBar.LayoutParams.MATCH_PARENT, height);

            final PopupWindow popup = popups;

            popup.setTouchable(true); // 设置popupwindow可点击
            popup.setOutsideTouchable(true); // 设置popupwindow外部可点击
            popup.setFocusable(true); // 获取焦点
            ColorDrawable cd = new ColorDrawable(-0000); // 设置背景
            popup.setBackgroundDrawable(cd);
            // final View roots = root;

            // popup.

            popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {

                    // TODO Auto-generated method stub
                    view.setEnabled(true);
                }
            });

            popup.setTouchInterceptor(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    /**** 如果点击了popupupwindow的外部，popupupwindow也会消失 ****/
					/*
					 * if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					 * popup.dismiss(); return true; } return false;
					 */
                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_OUTSIDE:
                            popup.dismiss();
                            return true;
                        case MotionEvent.ACTION_UP:
                            v.performClick();
                            break;
                    }
                    return false;
                }

            });

            return popup;
            // 级联显示 下拉框内容 end
        }
    }
    // 判断手机已安装某程序的方法：
    public static boolean isAvilible(Context context, String packageName) {

        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        List<String> pName = new ArrayList<String>();// 用于存储所有已安装程序的包名
        // 从pinfo中将包名字逐一取出，压入pName list中
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);// 判断pName中是否有目标程序的包名，有TRUE，没有FALSE
    }
}
